home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / utility / freedos.zip / COM050.ZIP / HISTORY.C < prev    next >
C/C++ Source or Header  |  1996-01-17  |  3KB  |  106 lines

  1. /* 
  2.  *  HISTORY.C - command line history.
  3.  *
  4.  *
  5.  *
  6.  *  Comments:
  7.  *
  8.  *  14/01/95 (Tim Norman) ---------------------------------------------------
  9.  *    started.
  10.  *
  11.  *  08/08/95 (Matt Rains) ---------------------------------------------------
  12.  *    i have cleaned up the source code. changes now bring this source into
  13.  *    guidelines for recommended programming practice.
  14.  */
  15.  
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <alloc.h>
  19.  
  20. #define MAXLINES 128
  21.  
  22. unsigned history_size = 256; /* make this configurable later */
  23.  
  24. void history(int dir, char *commandline)
  25. {
  26.    static char *history = NULL; 
  27.    static char *lines[MAXLINES];
  28.    static unsigned int curline = 0;
  29.    static unsigned int numlines = 0;
  30.    static unsigned int maxpos = 0;
  31.    int count; 
  32.    int length;
  33.  
  34.    if(!history)
  35.    {
  36.       history = malloc(history_size * sizeof(char));
  37.       lines[0] = history;
  38.       history[0] = 0;
  39.    }
  40.  
  41.    if(dir > 0) /* next command */
  42.    {
  43.       if(curline < numlines)
  44.       {
  45.          curline++;
  46.       }
  47.  
  48.       if(curline == numlines)
  49.       {
  50.          commandline[0] = 0;
  51.       }
  52.       else
  53.       {
  54.          strcpy(commandline, lines[curline]);
  55.       }
  56.    }
  57.    else if(dir < 0) /* prev command */
  58.    {
  59.       if(curline > 0)
  60.       {
  61.          curline--;
  62.       }
  63.  
  64.       strcpy(commandline, lines[curline]);
  65.    }
  66.    else /* add to history */
  67.    {
  68.       /* remove oldest string until there's enough room for next one */
  69.       /* strlen (commandline) must be less than history_size! */
  70.       while(maxpos + strlen (commandline) + 1 > history_size || numlines >= MAXLINES)
  71.       {
  72.          length = strlen(lines[0]) + 1;
  73.  
  74.          for(count = 0; count < maxpos && count + (lines[1] - lines[0]) < history_size; count++)
  75.          {
  76.             history[count] = history[count + length];
  77.          }
  78.  
  79.          maxpos -= length;
  80.  
  81.          for(count = 0; count <= numlines && count < MAXLINES; count++)
  82.          {
  83.             lines[count] = lines[count + 1] - length;
  84.          }
  85.  
  86.          numlines--;
  87. #ifdef DEBUG         
  88.          printf("Reduced size:  %ld lines\n", numlines);
  89.  
  90.          for(count = 0; count < numlines; count++)
  91.          {
  92.             printf("%d: %s\n", count, lines[count]);
  93.          }
  94. #endif
  95.       }
  96.  
  97.       strcpy(lines[numlines], commandline);
  98.       numlines++;
  99.       lines[numlines] = lines[numlines - 1] + strlen (commandline) + 1;
  100.       maxpos += strlen(commandline) + 1;
  101.       curline = numlines; /* last line, empty */
  102.    }
  103.  
  104.    return;
  105. }
  106.